var MagicDictionary = function () {
  this.list = []
}

/**
 * @param {string[]} dictionary
 * @return {void}
 */
MagicDictionary.prototype.buildDict = function (dictionary) {
  this.list = Array.from(new Set(dictionary))
}

/**
 * @param {string} searchWord
 * @return {boolean}
 */
MagicDictionary.prototype.search = function (searchWord) {
  for (const w of this.list) {
    if (w.length !== searchWord.length) continue
    if (this.compare(searchWord, w)) return true
  }
  return false
}

MagicDictionary.prototype.compare = function (w1, w2) {
  let diff = 0
  for (let i = 0; i < w1.length; i++) {
    if (w1[i] !== w2[i]) diff++
    if (diff > 1) return false
  }
  return diff === 1
}

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * var obj = new MagicDictionary()
 * obj.buildDict(dictionary)
 * var param_2 = obj.search(searchWord)
 */
